﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.css {
    import reprise.data.*;
    import reprise.utils.*;

    public class CSSParsingHelper {

        public static const preloadExpression:RegExp = /no[-]preload|preload/;
        public static const URIExpression:RegExp = /(?:url\([ ]*(['"]).*\1[ ]*\)|url\([ ]*[^'"][^)]*\))/;
        public static const durationExpression:RegExp = /(?:\d*[.]\d+m?s)|(?:\d+m?s)|0/;
        public static const percentageExpression:RegExp = /\d+%/;
        public static const repeatExpression:RegExp = /repeat[-]x|repeat[-]y|no[-]repeat|repeat/;
        public static const attachmentExpression:RegExp = /scroll|fixed/;
        public static const lengthExpression:RegExp = /\d+px|0/;
        public static const positionExpression:RegExp = new RegExp(((((((((("(?:(?:left|center|right|" + CSSParsingHelper.percentageExpression.source) + "|") + CSSParsingHelper.lengthExpression.source) + ")[ ]?(?:center|top|bottom|") + CSSParsingHelper.percentageExpression.source) + "|") + CSSParsingHelper.lengthExpression.source) + ")?)|") + "(?:(?:left|center|right|top|bottom)[ ]?(?:left|center|right|top|bottom)?)"));
        public static const propertyNameExpression:RegExp = /(?:[-]?\w+)+/;

        protected static var g_colorExpression:RegExp;

        public static function removeImportantFlagFromString(_arg1:String):Object{
            var _local2:Boolean;
            var _local3:int = _arg1.indexOf(CSSProperty.IMPORTANT_FLAG);
            if ((((_local3 > -1)) && ((_local3 == (_arg1.length - CSSProperty.IMPORTANT_FLAG.length))))){
                _local2 = true;
                _arg1 = _arg1.substr(0, -(CSSProperty.IMPORTANT_FLAG.length));
            };
            return ({important:_local2, result:_arg1});
        }
        public static function splitPropertyList(_arg1:String):Array{
            var _local5:int;
            var _local6:int;
            var _local7:int;
            var _local8:int;
            var _local9:int;
            var _local10:int;
            var _local2:Array = [];
            var _local3:int;
            var _local4:int;
            while (true) {
                _local5 = _arg1.indexOf("'", _local3);
                (((_local5 == -1)) && (99999));
                _local6 = _arg1.indexOf("\"", _local3);
                (((_local6 == -1)) && (99999));
                _local7 = _arg1.indexOf("(", _local3);
                (((_local7 == -1)) && (99999));
                _local8 = _arg1.indexOf(")", _local3);
                (((_local8 == -1)) && (99999));
                if ((((_local4 > 0)) && ((_local8 == 99999)))){
                    return (_local2);
                };
                if (_local4 == 0){
                    _local10 = _arg1.indexOf(",", _local3);
                    if (_local10 == -1){
                        _local2.push(_arg1);
                        return (_local2);
                    };
                    //unresolved if
                    _local2.push(_arg1.substr(0, _local10));
                    _arg1 = _arg1.substr((_local10 + 1));
                    _local3 = 0;
                } else {
                    if ((((((_local7 < _local5)) && ((_local7 < _local6)))) && ((_local7 < _local8)))){
                        _local4++;
                        _local3 = (_local7 + 1);
                    } else {
                        if ((((_local8 < _local5)) && ((_local8 < _local6)))){
                            _local4--;
                            _local3 = (_local8 + 1);
                        } else {
                            if (_local5 < _local6){
                                _local3 = (_local5 + 1);
                                while (true) {
                                    _local9 = _arg1.indexOf("'", _local3);
                                    if (_local9 == -1){
                                        return (_local2);
                                    };
                                    _local3 = (_local9 + 1);
                                    if (_arg1.charAt((_local9 - 1)) != "\\"){
                                        break;
                                    };
                                };
                            } else {
                                if (_local6 < _local5){
                                    _local3 = (_local6 + 1);
                                    while (true) {
                                        _local9 = _arg1.indexOf("\"", _local3);
                                        if (_local9 == -1){
                                            return (_local2);
                                        };
                                        _local3 = (_local9 + 1);
                                        if (_arg1.charAt((_local9 - 1)) != "\\"){
                                            break;
                                        };
                                    };
                                    continue;
                                };
                            };
                        };
                    };
                };
            };
            return (_local2);
        }
        public static function camelCaseCSSValueName(_arg1:String):String{
            var _local4:String;
            var _local2:Array = _arg1.split("-");
            var _local3:int = _local2.length;
            while (_local3-- > 1) {
                _local4 = _local2[_local3];
                _local2[_local3] = (_local4.charAt(0).toUpperCase() + _local4.substr(1));
            };
            return (_local2.join(""));
        }
        public static function resolvePathAgainstPath(_arg1:String, _arg2:String):String{
            var _local3:URL = new URL(_arg1);
            if (!_local3.isFileURL()){
                if (_local3.scheme() != null){
                    return (_arg1);
                };
                if (PathUtil.isAbsolutePath(_arg1)){
                    return (_arg1);
                };
            } else {
                _arg1 = _local3.path();
            };
            var _local4:URL = new URL(_arg2);
            var _local5:Boolean = ((!((_local4.scheme() == null))) || (_local4.isFileURL()));
            if (_local5){
                _arg2 = _local4.path();
            };
            if (PathUtil.pathExtension(_arg2) != ""){
                _arg2 = PathUtil.stringByDeletingLastPathComponent(_arg2);
            };
            var _local6:String = PathUtil.stringByResolvingRelativePathToPath(_arg1, _arg2);
            if (!_local5){
                return (_local6);
            };
            var _local7:String = ((_local4.host()) || (""));
            if (_local4.port()){
                _local7 = (_local7 + (":" + _local4.port()));
            };
            return (((((_local4.scheme()) || ("")) + _local7) + _local6));
        }
        public static function valueShouldInherit(_arg1:String):Boolean{
            return ((_arg1.indexOf(CSSProperty.INHERIT_FLAG) > -1));
        }
        public static function extractUnitFromString(_arg1:String):String{
            if (_arg1.indexOf(CSSProperty.UNIT_PIXEL) != -1){
                return (CSSProperty.UNIT_PIXEL);
            };
            if (_arg1.indexOf(CSSProperty.UNIT_PERCENT) != -1){
                return (CSSProperty.UNIT_PERCENT);
            };
            if (_arg1.indexOf(CSSProperty.UNIT_EM) != -1){
                return (CSSProperty.UNIT_EM);
            };
            return (null);
        }
        public static function valueIsColor(_arg1:String):Boolean{
            _arg1 = _arg1.toLowerCase();
            if ((((((((_arg1.indexOf("#") == 0)) || ((_arg1.indexOf("rgb") == 0)))) || ((_arg1 == "transparent")))) || (!((AdvancedColor.g_htmlColors[_arg1] == null))))){
                return (true);
            };
            return (false);
        }
        public static function get colorExpression():RegExp{
            var _local1:String;
            var _local2:Object;
            var _local3:String;
            if (!g_colorExpression){
                _local1 = ((("(?:#[0-9abcdefABCDEF]{8}|#[0-9abcdefABCDEF]{6}|" + "#[0-9abcdefABCDEF]{4}|#[0-9abcdefABCDEF]{3})(?!\\d)|") + "rgb\\(\\s*\\d+%?\\s*,\\s*\\d+%?\\s*,\\s*\\d+%?\\s*\\)|") + "rgba\\(\\s*\\d+%?\\s*,\\s*\\d+%?\\s*,\\s*\\d+%?\\s*,\\s*[0-9.]*%?\\s*\\)");
                _local2 = AdvancedColor.g_htmlColors;
                for (_local3 in _local2) {
                    _local1 = (_local1 + ("|" + _local3));
                };
                g_colorExpression = new RegExp(_local1);
            };
            return (g_colorExpression);
        }
        public static function parseColor(_arg1:String):AdvancedColor{
            var _local2:AdvancedColor = new AdvancedColor();
            _local2.setColorString(_arg1);
            return (_local2);
        }
        public static function parseURL(_arg1:String, _arg2:String):String{
            var _local3:String = StringUtil.stringBetweenMarkers(_arg1, "(", ")", true);
            if (_local3 == null){
                return (_arg1);
            };
            _local3 = resolvePathAgainstPath(_local3, _arg2);
            return (_local3);
        }
        public static function parseDeclarationString(_arg1:String, _arg2:String):CSSDeclaration{
            var _local5:Array;
            var _local6:String;
            var _local7:String;
            var _local3:CSSDeclaration = new CSSDeclaration();
            var _local4:RegExp = /([\w\-]+?)\s*[:]\s*(.+?)[;]/g;
            while (true) {
                _local5 = _local4.exec(_arg1);
                if (!_local5){
                    break;
                };
                _local6 = camelCaseCSSValueName(_local5[1]);
                _local7 = _local5[2];
                _local3.setValueForKeyDefinedInFile(_local7, _local6, _arg2);
            };
            return (_local3);
        }

    }
}//package reprise.css 
